package com.tepth.remote.core.net.callback;

import com.tepth.remote.core.app.Control;
import com.tepth.remote.core.net.loader.ControlLoader;
import com.tepth.remote.core.net.loader.LoaderStyle;

import org.ksoap2.serialization.SoapObject;

/**
 * Description:Soap工具回调处理
 *
 * @author Hequn.Lee
 * @date 2018/11/8
 */
public class SoapCallback {

    /**
     * 单例模式
     */
    private final static SoapCallback INSTANCE = new SoapCallback();

    /**
     * 让外部通过调用getInstance()方法来返回唯一的实例。
     *
     * @return 返回唯一实例
     */
    public static SoapCallback getInstance() {
        return INSTANCE;
    }

    /**
     * 回调处理
     *
     * @param result  返回的基础SoapObject
     * @param request 请求回调
     * @param success 请求成功回调
     * @param failure 请求失败回调
     * @param style   菊花样式
     */
    public void callback(final SoapObject result,
                         final IRequest request,
                         final ISoapSuccess success,
                         final IFailure failure,
                         final LoaderStyle style) {
        Control.getHandler().post(new Runnable() {
            @Override
            public void run() {
                //请求结束的回调
                if (request != null) {
                    request.onRequestEnd();
                }
                if (style != null) {
                    ControlLoader.closeLoading();
                }
                if (result != null) {
                    final SoapObject object = (SoapObject) result.getProperty(0);
                    if (object != null) {
                        success.onSuccess(object);
                    }
                } else {
                    failure.onFailure();
                }
            }
        });
    }
}
